﻿using System;
using System.Collections.Generic;
using System.Text;

namespace FlashPay.Service.Impl
{
    using FlashPay.DAO.Interface;
    using FlashPay.Entity;
    using FlashPay.Entity.Enum;
    using FlashPay.Entity.Parameter;
    using FlashPay.Entity.Request.Payment;
    using FlashPay.Entity.Response.Company;
    using FlashPay.Entity.Response.Payment;
    using FlashPay.Service.Interface;
    using FlashPay.Util;
    using Microsoft.AspNetCore.Http;
    using System.Linq;

    /// <summary>
    /// 实际付款业务接口实现		
    /// </summary>
    /// <remarks>2018-07-06 创建</remarks>
    public class RecordRealServiceImpl:RecordRealService
    {
        #region 注入
        /// <summary>
        /// 用户数据接口
        /// </summary>
        private readonly RecordRealDao _recordRealDao;

        /// <summary>
        /// 
        /// </summary>
        private readonly PaymentRecordDao _paymentRecordDao;

        /// <summary>
        /// 公司数据接口
        /// </summary>
        private readonly CompanyDao _companyDao;

        /// <summary>
        /// 银行数据接口
        /// </summary>
        private readonly BankDao _bankDao;

        /// <summary>
        /// 
        /// </summary>
        private readonly IHttpContextAccessor _accessor;

        /// <summary>
        /// 注入
        /// </summary>
        /// <param name="menu">菜单数据接口</param>
        public RecordRealServiceImpl(RecordRealDao recordRealDao, PaymentRecordDao paymentRecordDao, CompanyDao companyDao, BankDao bankDao, IHttpContextAccessor accessor)
        {
            _recordRealDao = recordRealDao;
            _paymentRecordDao = paymentRecordDao;
            _companyDao = companyDao;
            _bankDao = bankDao;
            _accessor = accessor;
        }
        #endregion

        /// <summary>
        /// 根据编号获取余额变化记录
        /// </summary>
        /// <param name="orderNo">编号</param>
        /// <returns>Company</returns>
        public JResult<RecordRealGetResponse> GetByOrderNo(long orderNo)
        {
            var result = new JResult<RecordRealGetResponse>() {
                Success = false
            };
            var recordReal = _recordRealDao.GetByOrderNo(orderNo);
            if (recordReal != null)
            {
                result.Success = true;
                result.Data = recordReal;
            }
            return result;
        }

        /// <summary>
        /// 转成功
        /// </summary>
        /// <param name="reqeust">参数</param>
        /// <returns></returns>
        public JResult TurnSuccess(TurnReqeust reqeust)
        {
            return _recordRealDao.TurnSuccess(reqeust);
        }

        /// <summary>
        /// (FlashPay)转成功
        /// </summary>
        /// <param name="reqeust">参数</param>
        /// <returns></returns>
        public JResult FlashPayTurnSuccess(TurnReqeust reqeust)
        {
            return _recordRealDao.FlashPayTurnSuccess(reqeust);
        }

        /// <summary>
        /// 转成功
        /// </summary>
        /// <param name="reqeust">参数</param>
        /// <returns></returns>
        public JResult EmptyRecordTurnSuccess(TurnReqeust reqeust)
        {
            return _recordRealDao.EmptyRecordTurnSuccess(reqeust);
        }
        

        /// <summary>
        /// 转失败
        /// </summary>
        /// <param name="reqeust">参数</param>
        /// <returns></returns>
        public JResult TurnFailure(TurnReqeust reqeust)
        {
            var paymentRecord = _recordRealDao.GetPaymentRecord(reqeust.OrderNo);
            if (paymentRecord == null)
            {
                throw new Exception("操作失败：付款订单不存在");
            }

            if (paymentRecord.PaymentStatus.Equals((sbyte)PaymentRecordPaymentStatus.付款中) && paymentRecord.PaymentType.Equals(1))
            {
                //var tlyPaymentRecord = _paymentRecordDao.TlyPaymentRecord(reqeust.OrderNo);
                //if (tlyPaymentRecord == null || tlyPaymentRecord.Data == null || tlyPaymentRecord.Data.IsRenewPayment == false || tlyPaymentRecord.Data.PaymentDateTimeDifference <= 5)
                //{
                //    return new JResult() {
                //        Success = false,
                //        ErrorMessage = "同略云订单请确认5分钟后重试"
                //    };
                //}
            }

            reqeust.Ip = _accessor.HttpContext.GetUserIp();

            return _recordRealDao.TurnFailure(reqeust);
        }

        /// <summary>
        /// 根据编号获取公司
        /// </summary>
        /// <param name="id">编号</param>
        /// <returns>Company</returns>
        private List<CompanyResponse> GetCompany(int companyId, int level)
        {
            var companyRequest = new EF.Models.Company()
            {
                CompanyId = companyId,
                CompanyPid = -1,
                CompanyStatus = 1
            };

            var companyList = new BaseModel<List<EF.Models.Company>>();

            _companyDao.Get(companyList, companyRequest, level);
            var companyResponse = new List<CompanyResponse>();
            if (companyList.Success)
            {
                foreach (var item in companyList.Result)
                {
                    companyResponse.Add(new CompanyResponse()
                    {
                        CompanyID = item.CompanyId,
                        CompanyName = item.CompanyName,
                        CompanyStatus = item.CompanyStatus
                    });
                }
            }
            return companyResponse;
        }

        /// <summary>
        /// 获取付款记录的真实付款记录
        /// </summary>
        /// <param name="query">查询参数</param>
        /// <returns></returns>
        public PaymentRecordRealResult<List<PaymentRecordRealResponse>> GetPaymentRecordReals(PaymentRecordRealQuery query)
        {
            var recordRealResponses = _recordRealDao.GetPaymentRecordReals(query);

            return recordRealResponses;
        }

        /// <summary>
        /// 已实际付款记录
        /// </summary>
        /// <param name="query">查询参数</param>
        public DataGrid<RecordRealResponse> GetPaymentRecordPager(RecordRealQuery query)
        {
            query.CompanyIds = _bankDao.GetCompanyIDList(query.CompanyId.Value);
            return _recordRealDao.GetPaymentRecordPager(query);
        }
    }
}
